package org.example.controller;

import org.example.bean.Orders;
import org.example.bean.Search;
import org.example.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("orders")
@ResponseBody
public class OrdersController {

    @Autowired
    private OrdersService ordersService;

    @GetMapping("ordersAll.do")
    public List<Orders> ordersAll(){
        return ordersService.ordersAll();
    }

    @GetMapping("{o_type}/{page}/{pageSize}/ordersType.do")
    public List<Orders> ordersType(@PathVariable String o_type,@PathVariable Integer page,@PathVariable Integer pageSize){

        return ordersService.ordersType(o_type,page,pageSize);
    }

    @GetMapping("{o_type}/{page}/{pageSize}/ordersTypeThen.do")
    public List<Orders> ordersTypeThen(@PathVariable String o_type,@PathVariable Integer page,@PathVariable Integer pageSize){

        return ordersService.ordersTypeThen(o_type,page,pageSize);
    }

    @GetMapping("{o_type}/{supplier}/{page}/{pageSize}/ordersTypeScore.do")
    public List<Orders> ordersTypeScore(@PathVariable String o_type,@PathVariable String supplier,@PathVariable Integer page,@PathVariable Integer pageSize){

        return ordersService.ordersTypeScore(supplier,o_type,page,pageSize);
    }

    @GetMapping("{o_type}/total.do")
    public Integer total(@PathVariable String o_type){
        return ordersService.total(o_type);
    }

    @GetMapping("totalThen.do")
    public Integer totalThen(){
        return ordersService.totalThen();
    }

    @GetMapping("{o_type}/{supplier}/totalScore.do")
    public Integer totalScore(@PathVariable String o_type,@PathVariable String supplier){
        return ordersService.totalScore(supplier,o_type);
    }

    //根据o_id和o_type修改o_type
    @GetMapping("{o_type}/{o_id}/update.do")
    public boolean update(@PathVariable String o_type,@PathVariable Integer o_id){
        return ordersService.update(o_type,o_id)>0;
    }
    @GetMapping("searchAll.do")
    public List<Orders> searchAll(Search search){
        return ordersService.searchAll(search);
    }

    @GetMapping("searchAllThen.do")
    public List<Orders> searchAllThen(Search search){
        return ordersService.searchAllThen(search);
    }

    @GetMapping("{type}/{o_id}/jy.do")
    public List<Orders> jy(@PathVariable String type,@PathVariable Integer o_id){
        return ordersService.jy(type,o_id);
    }

    @GetMapping("fuel.do")
    public boolean fuel(@RequestParam Integer o_id,@RequestParam String f,@RequestParam String breed){
        System.out.println(o_id);
        System.out.println(f);
        System.out.println(breed);
        String[] fs=f.split(",");
        Integer[] ff=new Integer[fs.length];
        String[] bs=breed.split(",");
        String[] breeds= new String[bs.length];
        for (int i = 0; i < fs.length; i++) {
            breeds[i]=bs[i].replace("\"","");
            ff[i]= Integer.valueOf(fs[i].replace("\"",""));
        }

        return ordersService.fuel(o_id,ff,breeds);
    }

    @GetMapping("complete.do")
    public boolean complete(@RequestParam Integer o_id,@RequestParam String f,@RequestParam String breed){
        System.out.println(o_id);
        System.out.println(f);
        System.out.println(breed);
        String[] fs=f.split(",");
        Integer[] ff=new Integer[fs.length];
        String[] bs=breed.split(",");
        String[] breeds= new String[bs.length];
        for (int i = 0; i < fs.length; i++) {
            breeds[i]=bs[i].replace("\"","");
            ff[i]= Integer.valueOf(fs[i].replace("\"",""));
        }

        return ordersService.complete(o_id,ff,breeds);
    }
    @GetMapping("pf.do")
    public boolean pf(@RequestParam Integer o_id,@RequestParam Integer oil,@RequestParam Integer serve,@RequestParam Integer other){
        return ordersService.pf(o_id,oil,serve,other);
    }

    @GetMapping("yc.do")
    public boolean yc(@RequestParam Integer o_id,@RequestParam String info){
        System.out.println("info="+info);
        return ordersService.yc(o_id,info);
    }

    //根据o_id查数据
    @GetMapping("idSer.do")
    public Orders idSer(Integer o_id){
        return ordersService.idSer(o_id);
    }

    //根据o_id修改供应商数据
    @GetMapping("updateSupplier.do")
    public Integer updateSupplier(@RequestParam Integer o_id,@RequestParam String supplier){
        return ordersService.updateSupplier(o_id,supplier);
    }

}
